package com.itqf.ibatis;

import com.itqf.bean.Menu;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * projectName: mybatis2103
 *
 * @author: 赵伟风
 * time: 2021/6/23 17:43
 * description:ibatis的方式进行插入
 */
public class UseIbatisUpdate {

    public static void main(String[] args) throws Exception {

        //1.读取配置
        InputStream stream = Resources.getResourceAsStream("mybatis_config.xml");
        //2.创建sqlsesssionfactorybuilder
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        //3.创建sqlsessionfactory
        SqlSessionFactory sqlSessionFactory = factoryBuilder.build(stream);
        //4.创建sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //自动开启事务!
        //5.调用插入数据方法
        Menu menu = new Menu();
        menu.setmId(1);
        menu.setmInfo("不知道");
        menu.setmName("也不知道");


        int rows = sqlSession.insert("com.itqf.mybatis.updateMenu", menu);

        if (rows>0) {
            System.out.println("修改数据成功!");
        }

        System.out.println(menu);
        //6.关闭资源
        sqlSession.commit();
        sqlSession.close();


    }


}
